package com.learnyou.server.xingzuo.controller.common.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import com.learnyou.server.xingzuo.biz.common.CosFileBiz;
import com.learnyou.server.xingzuo.biz.helper.MediaSaveObject;
import com.learnyou.server.xingzuo.common.constants.ErrorCode;
import com.learnyou.server.xingzuo.common.exception.LearnyouBootException;
import com.learnyou.server.xingzuo.controller.common.XinzuoMediaResource;
import com.qcloud.cos.model.COSObjectInputStream;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;

@RestController
public class XinzuoMediaController implements XinzuoMediaResource {

    @Resource
    private CosFileBiz cosFileBiz;

    @Override
    public void getMedia(String target, HttpServletResponse response) {
        try {
            COSObjectInputStream inputStream = cosFileBiz.readFile(target);
            String contentType = target.indexOf("png")>0 ? MediaType.IMAGE_PNG_VALUE: MediaType.IMAGE_JPEG_VALUE;
            response.setContentType(contentType);
            //循环写入输出流
            byte[] b = new byte[1024];
            int length;
            //打开本地文件流
            OutputStream os = response.getOutputStream();
            while ((length = inputStream.read(b)) > 0) {
                os.write(b, 0, length);
            }
            os.flush();
            os.close();
            inputStream.close();
        } catch (IOException ex) {
            throw new LearnyouBootException(ErrorCode.FILE_READ_ERROR);

        }
    }

    @Override
    public String putMedia(String mediaSource, MultipartFile mediaFile) {
        String fileName = mediaFile.getOriginalFilename();

        String fileSuffix = fileName.substring(fileName.indexOf("."));
        String targetFileName = mediaSource + "_" + System.currentTimeMillis() + fileSuffix;

        try {
            cosFileBiz.saveFile(targetFileName, mediaFile.getInputStream(), mediaFile.getSize());
            return targetFileName;
        } catch (Exception ex) {
            throw new LearnyouBootException(ErrorCode.FILE_READ_ERROR);
        }
    }

    @Override
    public String putImageMedia(String mediaSource, MultipartFile mediaFile) {
        String fileName = mediaFile.getOriginalFilename();

        String fileSuffix = FileUtil.extName(fileName);
        String targetFileName = mediaSource + "_" + System.currentTimeMillis()+"." + fileSuffix;
        try {
            MediaSaveObject mediaSaveObject = new MediaSaveObject();
            mediaSaveObject.setSaveTo(targetFileName);
            mediaSaveObject.setSize(mediaFile.getSize());
            mediaSaveObject.setInputStream(mediaFile.getInputStream());
            cosFileBiz.saveImageFile(CollUtil.newArrayList(mediaSaveObject));
            return targetFileName;
        } catch (Exception ex) {
            throw new LearnyouBootException(ErrorCode.FILE_READ_ERROR);
        }
    }

    @Override
    public boolean removeMedia(String target) {
        return cosFileBiz.removeFile(target);
    }
}
